package com.zhihesj.anan.message;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author anan.
 * @version 1.0
 * @create 2020-07-25 17-03
 */
@Data
@Component
@ApiModel(value="全局统一返回结果")
public class Message<T> {

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回Token")
    private String token;

    public static Message success(Object object) {
        Message message = new Message();
        message.setCode(MessageCodeEnum.SUCCESS.getCode());
        message.setMessage(MessageCodeEnum.SUCCESS.getMessage());
        message.setData(object);
        message.setSuccess(true);
        return message;
    }

    public static Message success(MessageCodeEnum messageCodeEnum) {
        Message message = new Message();
        message.setCode(messageCodeEnum.getCode());
        message.setMessage(messageCodeEnum.getMessage());
        message.setData(messageCodeEnum.toString());
        message.setSuccess(true);
        return message;
    }

    public static Message success(){
        return success(MessageCodeEnum.SUCCESS);
    }


    public static Message token(String token){
        Message message =new Message();
        message.setCode(MessageCodeEnum.SUCCESS.getCode());
        message.setMessage(MessageCodeEnum.SUCCESS.getMessage());
        message.setToken(token);
        message.setSuccess(true);
        return message;
    }

    public static Message setResult(MessageCodeEnum messageCodeEnum){
        Message message =new Message();
        message.setCode(messageCodeEnum.getCode());
        message.setMessage(messageCodeEnum.getMessage());
        message.setSuccess(messageCodeEnum.getSuccess());
        return message;
    }

    public static Message error(MessageCodeEnum messageCodeEnum){
        Message message =new Message();
        message.setCode(messageCodeEnum.getCode());
        message.setMessage(messageCodeEnum.getMessage());
        message.setSuccess(messageCodeEnum.getSuccess());
        return message;
    }


    public static Message error(String error){
        Message message =new Message();
        message.setCode(MessageCodeEnum.UNKNOWN_REASON.getCode());
        message.setMessage(error);
        message.setSuccess(MessageCodeEnum.UNKNOWN_REASON.getSuccess());
        return message;
    }

}